Strings
பைத்தானில் சரங்கள் ஒற்றை மேற்கோள் குறிகள் அல்லது இரட்டை மேற்கோள் குறிகளால் சூழப்பட்டுள்ளன.
'hello' என்பது "hello" போன்றதே.
நீங்கள் print() செயல்பாட்டுடன் ஒரு சர மாறிலியைக் காட்டலாம்:
Example
print("Hello")
print('Hello')
Quotes Inside Quotes
நீங்கள் சரத்திற்குள் மேற்கோள்களைப் பயன்படுத்தலாம், அவை சரத்தைச் சுற்றியுள்ள மேற்கோள்களுடன் பொருந்தாத வரை:
Example
print("It's alright")
print("He is called 'Johnny'")
print('He is called "Johnny"')
Assign String to a Variable
ஒரு சரத்தை ஒரு மாறிக்கு ஒதுக்குவது மாறி பெயரைத் தொடர்ந்து சம அடையாளம் மற்றும் சரத்துடன் செய்யப்படுகிறது:
Example
a = "Hello"
print(a)
Multiline Strings
மூன்று மேற்கோள்களைப் பயன்படுத்தி பல வரி சரத்தை ஒரு மாறிக்கு ஒதுக்கலாம்:
Example
a = """Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."""
print(a)
அல்லது மூன்று ஒற்றை மேற்கோள்கள்:
Example
a = '''Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.'''
print(a)
குறிப்பு: முடிவில், வரி முறிவுகள் குறியீட்டில் உள்ள அதே நிலையில் செருகப்படுகின்றன.
Strings are Arrays
பல பிற பிரபலமான நிரலாக்க மொழிகளைப் போலவே, பைத்தானில் சரங்கள் யுனிகோட் எழுத்துக்களின் வரிசைகள் ஆகும்.
இருப்பினும், பைத்தானில் ஒரு எழுத்து தரவு வகை இல்லை, ஒரு ஒற்றை எழுத்து என்பது நீளம் 1 கொண்ட ஒரு சரம் மட்டுமே.
சரத்தின் உறுப்புகளை அணுக சதுர அடைப்புக்குறிகளைப் பயன்படுத்தலாம்.
Example
a = "Hello, World!"
print(a[1])
Looping Through a String
சரங்கள் வரிசைகள் என்பதால், for சுழற்சியுடன் ஒரு சரத்தில் உள்ள எழுத்துக்கள் வழியாக சுழல முடியும்.
Example
for x in "banana":
print(x)
எங்கள் பைத்தான் For Loops அத்தியாயத்தில் For Loops பற்றி மேலும் அறிக.
String Length
ஒரு சரத்தின் நீளத்தைப் பெற, len() செயல்பாட்டைப் பயன்படுத்தவும்.
Example
a = "Hello, World!"
print(len(a))
Check String
ஒரு குறிப்பிட்ட சொற்றொடர் அல்லது எழுத்து ஒரு சரத்தில் உள்ளதா என சரிபார்க்க, in விசேஷ சொல்லைப் பயன்படுத்தலாம்.
Example
txt = "The best things in life are free!"
print("free" in txt)
if கூற்றில் இதைப் பயன்படுத்தவும்:
Example
txt = "The best things in life are free!"
if "free" in txt:
print("Yes, 'free' is present.")
எங்கள் பைத்தான் If...Else அத்தியாயத்தில் If கூற்றுகள் பற்றி மேலும் அறிக.
Check if NOT
ஒரு குறிப்பிட்ட சொற்றொடர் அல்லது எழுத்து ஒரு சரத்தில் இல்லை என சரிபார்க்க, not in விசேஷ சொல்லைப் பயன்படுத்தலாம்.
Example
txt = "The best things in life are free!"
print("expensive" not in txt)
if கூற்றில் இதைப் பயன்படுத்தவும்:
Example
txt = "The best things in life are free!"
if "expensive" not in txt:
print("No, 'expensive' is NOT present.")